package MyCustomTable;

import MyUtil.DataBaseUtil;
import MyUtil.JScrollUtil;
import MyUtil.MyPanel;
import MyUtil.ResultSetTableModel;

import java.awt.*;
import java.sql.*;
import javax.swing.*;

public class Book_dynamicQuery extends JFrame{
		// 数据库相关
		private JScrollPane scrollPane;
		private String sql="select * from Book where";
		private Boolean t1 = true;

		//建立二个控件
		private JPanel jpanel1 = new MyPanel("/picture/5.jpg");

		//空间二的内容
				//书名
			JCheckBox jc1 = new JCheckBox("书名");
			JTextField jt1 = new JTextField(10);
				//出版社
			JCheckBox jc2 = new JCheckBox("出版社");
			JTextField jt2 = new JTextField(20);
				//作者
			JCheckBox jc3 = new JCheckBox("作者");
			JTextField jt3 = new JTextField(10);
				//类别
			JCheckBox jc4 = new JCheckBox("类别");
			JTextField jt4 = new JTextField(10);
				//按钮
			JButton jb1 = new JButton("查询");
		JTextArea textArea1	= new JTextArea();

   		public Book_dynamicQuery(String name) throws SQLException {
			//设置总JFrame属性
			super(name);
			scrollPane = new JScrollPane();
			setBounds(400, 200, 800, 700);
			setContentPane(jpanel1);
			//设置中间文本框
			jpanel1.add(textArea1);

			//设置控件二
			jpanel1.add(jc1);
			jpanel1.add(jt1);
			jpanel1.add(jc2);
			jpanel1.add(jt2);
			jpanel1.add(jc3);
			jpanel1.add(jt3);
			jpanel1.add(jc4);
			jpanel1.add(jt4);
			jpanel1.add(jb1);
			jt1.setText("");
			jt2.setText("");
			jt3.setText("");
			jt4.setText("");
			this.setLayout(new CustomLayout());
			jb1.addActionListener((e) -> {
				textArea1.setText("");
				if (jc1.isSelected()) {
					if ((jt1.getText()).equals("")) {
						JOptionPane.showMessageDialog(this, "缺少输入数据!");
						t1 = false;
					} else {
						sql += " and" + " (Bname ='" + jt1.getText() + "')";
					}
				}
				if (jc2.isSelected()) {
					if ((jt2.getText()).equals("")) {
						JOptionPane.showMessageDialog(this, "缺少输入数据!");
						t1 = false;
					} else {
						sql += " and" + " (Bpress ='" + jt2.getText() + "')";
					}
				}
				if (jc3.isSelected()) {
					if (jt3.getText().equals("")) {
						JOptionPane.showMessageDialog(this, "缺少输入数据!");
						t1 = false;
					} else {
						sql += " and" + " (Bauthor ='" + jt3.getText() + "')";
					}
				}
				if (jc4.isSelected()) {
					if ((jt4.getText()).equals("")) {
						JOptionPane.showMessageDialog(this, "输入错误!");
						t1 = false;
					} else {
						sql += " and" + " (Btype ='" + jt4.getText() + "')";
					}
				}

				if (sql.endsWith("where")) {
					if (t1) {
						sql = sql.replace("where", "");
						textArea1.append(sql);
					}
				} else {
					if (t1) {
						sql = sql.replaceFirst(" and", "");
						textArea1.append(sql);
					}
				}
				if (t1) {
					try {
						jpanel1.remove(scrollPane);
						scrollPane = JScrollUtil.getCustomJScrollPane(DataBaseUtil.produceResultSet(sql));
						jpanel1.add(scrollPane);
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
				sql = "Select * from Book where";
				t1 = true;
			});
		}
		private class CustomLayout implements  LayoutManager{

			@Override
			public void addLayoutComponent(String name, Component comp) {

			}

			@Override
			public void removeLayoutComponent(Component comp) {

			}

			@Override
			public Dimension preferredLayoutSize(Container parent) {
				return null;
			}

			@Override
			public Dimension minimumLayoutSize(Container parent) {
				return null;
			}

			@Override
			public void layoutContainer(Container parent) {
				int width = getWidth();
				int height = getHeight();
				if(jc1.isVisible()){
					jc1.setBounds(width/8, height/8,100,25);
				}
				if(jt1.isVisible()){
					jt1.setBounds(width/8+100, height/8,100,25);
				}
				if(jc2.isVisible()){
					jc2.setBounds(width/8, height/8+25,100,25);
				}
				if(jt2.isVisible()){
					jt2.setBounds(width/8+100, height/8+25,100,25);
				}
				if(jc3.isVisible()){
					jc3.setBounds(width/8, height/8+50,100,25);
				}
				if(jt3.isVisible()) {
					jt3.setBounds(width / 8 + 100, height / 8 + 50, 100, 25);
				}
				if(jc4.isVisible()){
					jc4.setBounds(width/8,height/8+75,100,25);
				}
				if(jt4.isVisible()){
					jt4.setBounds(width/8+100, height/8+75,100,25);
				}
				if(jb1.isVisible()){
					jb1.setBounds(7*width/8-70,height/11,70,height/6);
				}
				if (textArea1.isVisible()){
					textArea1.setBounds(width/8,height*5/17,width*3/4,height/5);
				}
				if(!scrollPane.isValid()){
					scrollPane.setBounds(0,height/2,width,height/2);
				}
			}
   		}
}